package interview.sync.lock;/*

 */

/**
 * @ClassName ThreadDemo
 * @Author Final
 * 受用syncchronized
 * @Project DataStructures
 */

class Share1{
    private int number =0;
    public synchronized  void incr() throws InterruptedException {
        while (number!=0){
            this.wait();;
        }
        number++;
        System.out.println(Thread.currentThread().getName()+number);
        this.notifyAll();
    }

    public synchronized  void decr() throws InterruptedException {
        while (number!=1){
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+number);
        this.notifyAll();
    }
}
public class ThreadDemo {
    public static void main(String[] args) {

        Share1 share=new Share1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<30;i++){
                    try {
                        share.incr();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"cxm").start();

        new Thread(()->{
            for (int i=0;i<10;i++){
                try {
                    share.incr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"aa").start();

        new Thread(()->{
            for (int i=0;i<10;i++){
                try {
                    share.decr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"bb").start();
    }
}
